package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-03-06 22:01
 */
public class TreeToDoublyList {
    private Node1 pre, head;

    public Node1 treeToDoublyList(Node1 root) {
        if (root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    private void dfs(Node1 cur) {
        if (cur == null) return;
        dfs(cur.left);
        if (pre==null) head = cur;
        else pre.right=cur;
        cur.left=pre;
        pre = cur;
        dfs(cur.right);
    }
}
